package com.zh.tea.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zh.tea.dao.TeaMapper;
import com.zh.tea.dao.TypeMapper;
import com.zh.tea.entity.Tea;
import com.zh.tea.entity.TeaExample;
import com.zh.tea.entity.Type;
import com.zh.tea.entity.TypeExample;
import com.zh.tea.service.TeaService;
import com.zh.tea.utils.SessionFactoryUtils;

public class TeaServiceImpl implements TeaService {
		
	/**
	 * 添加茶品
	 * author:006
	 */
	@Override
	public int addTea(Tea tea) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TeaMapper teamapper=session.getMapper(TeaMapper.class);
		int n=teamapper.insert(tea);
		session.commit();
		session.close();
		return n;
	}
	/**
	 * 查询茶品
	 */

	@Override
	public PageInfo<Tea> getTea(Tea tea,Integer pagNo,Integer pageSize) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TeaMapper teamapper=session.getMapper(TeaMapper.class);
		TeaExample teaexm=new TeaExample();
		TeaExample.Criteria criteria=teaexm.createCriteria();
		if(tea!=null){
		if(tea.getTypeid()!=0){
			criteria.andTypeidEqualTo(tea.getTypeid());
		}
		}
		PageHelper.startPage(pagNo, pageSize);
		List<Tea> listt=teamapper.selectByExample(teaexm);
		System.out.println(listt.isEmpty());
		PageInfo<Tea> pagetea=new PageInfo<>(listt); 
		session.close();
		return pagetea;
	}
	//按照id查询 tea
	public Tea getTeaById(Integer id){
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TeaMapper team=session.getMapper(TeaMapper.class);
		Tea tea=team.selectByPrimaryKey(id);
		session.close();
		
		return tea;
	}
	//按照teano删除茶的信息
	@Override
	public int delTea(Tea tea) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TeaMapper team=session.getMapper(TeaMapper.class);
		/*int n=team.deleteByPrimaryKey(tea.getId());*/
		TeaExample teaexm=new TeaExample();
		TeaExample.Criteria criteria=teaexm.createCriteria();
		criteria.andTeanoEqualTo(tea.getTeano());
		int n= team.deleteByExample(teaexm);
		session.commit();
		return n;
	}

	//按照teaid 更新tea的信息
	@Override
	public int updTea(Tea tea) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TeaMapper team=session.getMapper(TeaMapper.class);
		TeaExample teaexm=new TeaExample();
		TeaExample.Criteria criteria=teaexm.createCriteria();
	
		criteria.andBrandidEqualTo(tea.getBrandid());
		criteria.andTypeidEqualTo(tea.getTypeid());
		criteria.andTeanoEqualTo(tea.getTeano());
		criteria.andNameEqualTo(tea.getName());
		criteria.andPriceEqualTo(tea.getPrice());
		criteria.andStockEqualTo(tea.getStock());
		criteria.andPackspecEqualTo(tea.getPackspec());
		criteria.andBzqEqualTo(tea.getBzq());
		criteria.andTechEqualTo(tea.getTech());
		
		/*Tea ntea=new Tea();*/
		/*//需要更改的信息
		if(tea.getBrandid()!=null){
		ntea.setBrandid(tea.getBrandid());
		}
		if(tea.getBzq()!=null){
			ntea.setBzq(tea.getBzq());
		}
		if(tea.getLicenseno()!=null){
			ntea.setLicenseno(tea.getLicenseno());
		}
		if(tea.getName()!=null){
			ntea.setName(tea.getName());
		}
		if(tea.getPackspec()!=null){
			ntea.setPackspec(tea.getPackspec());
		}
		if(tea.getPic1()!=null){
			ntea.setPic1(tea.getPic1());
		}
		if(tea.getPrice()!=null){
			ntea.setPrice(tea.getPrice());
		}
		if(tea.getProducedate()!=null){
			ntea.setProducedate(tea.getProducedate());
		}
		if(tea.getProloc()!=null){
			ntea.setProloc(tea.getProloc());
		}
		if(tea.getTypeid()!=null){
			ntea.setTypeid(tea.getTypeid());
		}*/
		/*int a=team.updateByExample(tea, teaexm);*/
		
		int n= team.updateByExampleSelective(tea, teaexm);
		/*int n= team.updateByExample(tea, teaexm);*/
		session.commit();
		session.close();
		return n;
	}
	@Override
	public Tea selectOneTea(String teano) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TeaMapper team=session.getMapper(TeaMapper.class);
		TeaExample teaexm=new TeaExample();
		TeaExample.Criteria criteria=teaexm.createCriteria();
		criteria.andTeanoEqualTo(teano);
		List<Tea> tealist=team.selectByExample(teaexm);
		Tea tea=tealist.get(0);
		session.close();
		return tea;
	}
	@Override
	public List<Tea> selectTeaByOr(String word, Integer pageNo, Integer pageSize) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TeaMapper team=session.getMapper(TeaMapper.class);
		//搜索名字包含该字母的tea数据
		TeaExample nameexam=new TeaExample();
		TeaExample.Criteria namecri=nameexam.createCriteria();
		namecri.andNameLike("%"+word+"%");
		List<Tea> listname=team.selectByExample(nameexam);
		//按照产地搜索数据
		TeaExample prolocexam=new TeaExample();
		TeaExample.Criteria proloccri=prolocexam.createCriteria();
		proloccri.andProlocLike("%"+word+"%");
		List<Tea> listproloc=team.selectByExample(prolocexam);
		//按照品类搜索数据
		//1.搜索出来相应品类的编号
		TypeMapper typem=session.getMapper(TypeMapper.class);
		TypeExample typeexm=new TypeExample();
		TypeExample.Criteria typecri=typeexm.createCriteria();
		typecri.andNameLike("%"+word+"%");
		List<Type> listType=typem.selectByExample(typeexm);
		List<Integer> listtypeid=new ArrayList<>();
		for(Type type:listType){
			listtypeid.add(type.getId());
		}
		//2.搜索出来再这个编号内的茶品
		TeaExample typeidexm= new TeaExample();
		TeaExample.Criteria typeidcri=typeidexm.createCriteria();
		typeidcri.andTypeidIn(listtypeid);
		List<Tea> liitteabytypeid=team.selectByExample(typeidexm);
		//判断产地查询和名字查询是否包含相同的 tea
		for(Tea tea:listproloc){
			if(!listname.contains(tea)){
				listname.add(tea);
			}
		}
		//判断类型查询和以上结果集是否包含相同的结果集
		for(Tea tea:liitteabytypeid){
			if(!listname.contains(tea)){
				listname.add(tea);
			}
		}
		session.close();
		return listname;
	}
	@Override
	public PageInfo<Tea> selectTeaByAnd(Tea tea, Integer pageNo, Integer pageSize) {
		// TODO Auto-generated method stub
		SqlSession session =SessionFactoryUtils.getSqlSessionFactory().openSession();
		TeaMapper team=session.getMapper(TeaMapper.class);
		TeaExample teaexm=new TeaExample();
		TeaExample.Criteria teacri=teaexm.createCriteria();
		if(tea!=null){
		if(tea.getProloc()!=null){
			teacri.andProlocEqualTo(tea.getProloc());
		}
		if(tea.getBrandid()!=null){
			teacri.andBrandidEqualTo(tea.getBrandid());
		}
		if(tea.getTypeid()!=null){
			teacri.andTypeidEqualTo(tea.getTypeid());
		}
		}
		PageHelper.startPage(pageNo,pageSize);
		List<Tea> listtea=team.selectByExample(teaexm);
		PageInfo<Tea> pagetea=new PageInfo(listtea);
		
		return pagetea;
	}
	
	@Override
	public Tea selectTeaname(String name) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TeaMapper team=session.getMapper(TeaMapper.class);
		TeaExample teaexm=new TeaExample();
		TeaExample.Criteria criteria=teaexm.createCriteria();
		criteria.andNameEqualTo(name);
		List<Tea> tealist=team.selectByExample(teaexm);
		Tea tea=tealist.get(0);
		return tea;
	}

}
